//
//  NotNetworkToolView.m
//  GCHCustomerMall
//
//  Created by 观潮汇 on 2016/11/13.
//  Copyright © 2016年 观潮汇. All rights reserved.
//

#import "NotNetworkToolView.h"

#define NotNetworkAlertMessage     @"网络请求失败，请检查您的网络设置"
#define NotNetworkAlertFont        [UIFont systemFontOfSize:13]
#define NotNetworkAlertColor       [UIColor whiteColor]


@interface NotNetworkToolView ()

@property (strong, nonatomic) UILabel *alertLB;
@property (strong, nonatomic) UIImageView *wifiImage;
@property (strong, nonatomic) UIImageView *arrowImage;

@end

static NotNetworkToolView *notNetworkToolView = nil;

@implementation NotNetworkToolView

+ (instancetype)shareNotNetworkToolView
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        notNetworkToolView = [[NotNetworkToolView alloc] init];
    });
    return notNetworkToolView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self initSubview];
    }
    return self;
}

- (UILabel *)alertLB
{
    if (!_alertLB) {
        _alertLB = [[UILabel alloc] init];
        _alertLB.font = NotNetworkAlertFont;
        _alertLB.textColor = NotNetworkAlertColor;
        _alertLB.text = NotNetworkAlertMessage;
        _alertLB.frame = CGRectMake(self.bounds.size.height, 0, self.bounds.size.width - 2*self.bounds.size.height, self.bounds.size.height);
        _alertLB.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
    }
    return _alertLB;
}

- (UIImageView *)wifiImage
{
    if (!_wifiImage) {
        _wifiImage = [[UIImageView alloc] init];
        _wifiImage.image = [UIImage imageNamed:@"icon_wifi.png"];
        _wifiImage.contentMode = UIViewContentModeCenter;
        _wifiImage.frame = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height);
        _wifiImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
    }
    return _wifiImage;
}

- (UIImageView *)arrowImage
{
    if (!_arrowImage) {
        _arrowImage = [[UIImageView alloc] init];
        _arrowImage.image = [UIImage imageNamed:@"arrow_right_white.png"];
        _arrowImage.contentMode = UIViewContentModeCenter;
        _arrowImage.frame = CGRectMake(self.bounds.size.width-self.bounds.size.height, 0, self.bounds.size.height, self.bounds.size.height);
        _arrowImage.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
    }
    return _arrowImage;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

- (void)initSubview
{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6];
    self.frame = CGRectMake(0, kScreenDrawIOS7FrameY, kScreen_Width, kTopBarHeight);
    
    [self addSubview:self.alertLB];
    [self addSubview:self.wifiImage];
    [self addSubview:self.arrowImage];
}

- (void)show
{
    if (self.superview == nil) {
        [[Utility mainWindow] addSubview:self];
    }
    self.hidden = NO;
}

- (void)hide
{
    self.hidden = YES;
}
@end
